package br.com.iswe.gwt.core.server;

import javax.persistence.Transient;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Classe base para RemoteServiceServlet's que utilizam Spring.
 * 
 * @author Rafael Noronha
 */
@SuppressWarnings("serial")
public class SpringGwtServiceServlet extends RemoteServiceServlet {
	
	private ServletContext servletContext = null;
	
	//Classe de Log
    protected static transient Log log = getLog();
    @Transient
	protected static Log getLog() {
        return LogFactory.getLog(SpringGwtServiceServlet.class);
    }
	
	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		super.init(servletConfig);
		this.servletContext = servletConfig.getServletContext();
	}
	
	protected BeanFactory getBeanFactory(){
		WebApplicationContext wac = null;
		wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		return wac;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	protected HttpServletRequest getRequest(){
		return (HttpServletRequest)getThreadLocalRequest();
	}
	
	protected HttpSession getSession(){
		return getRequest().getSession();
	}
}
